package com.cool.task;

import java.util.concurrent.CompletableFuture;

import jakarta.annotation.Resource;
import org.quartz.DisallowConcurrentExecution;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import com.cool.modelmapper.model.Apple;
import com.cool.modelmapper.model.Fruit;
import com.cool.mybatisplus.SysMenuDaoService;
import com.cool.mybatisplus.entity.SystemMenu;
import com.cool.pojo.CommonConstants;
import com.cool.service.CommonService;
import com.cool.service.DtoService;
import com.cool.service.ExportService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author bian
 * @date 2021-02-09
 */
@Slf4j
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@EnableScheduling
@DisallowConcurrentExecution
public class ScheduledTask {
    private final CommonService commonService;

    private final ExportService exportService;

    private final DtoService dtoService;

    // private final RedisTemplate<String, String> redisTemplate;

    private final SysMenuDaoService sysMenuDaoService;

    @Resource(name = "customThreadPool")
    private ThreadPoolTaskExecutor customThreadPoolExecutor;


    /**
     * 10s执行一次,测试责任链模式
     */
    // @Scheduled(cron = CommonConstants.CRON)
    private void run() {
        log.info("测试log");
        commonService.handle();
    }

    /**
     * 10s执行一次,测试导出Excel
     */
    // @Scheduled(cron = CommonConstants.EXPORT_CRON)
    private void export() {
        exportService.export();
    }

    // @Scheduled(cron = CommonConstants.EXPORT_CRON)
    private void dto() {
        Fruit fruit = Fruit.builder().color("蓝色").origin("巴西").weight(10).build();
        Apple apple = dtoService.tran(fruit);
        System.out.println(apple);
    }

    @Scheduled(cron = CommonConstants.REDIS_CRON)
    private void cache() {
        Fruit fruit = Fruit.builder().color("蓝色").origin("巴西").weight(10).build();
        Apple apple = new Apple();
        BeanUtils.copyProperties(fruit, apple);
        // System.out.println(fruitFromRedis);
        System.out.println(apple);
    }

    // @Scheduled(cron = CommonConstants.MYBATIS_CRON)
    private void testMybatis() {
        int count = sysMenuDaoService.queryTotal();
        int count2 = sysMenuDaoService.queryTotal2();
        // List<SystemMenu> systemMenus = sysMenuDaoService.getAllSystemMenu();
        // List<SystemMenu> systemMenus1 = sysMenuDaoService.selectList(new QueryWrapper<>());
        // SystemMenu systemMenu =
        // SystemMenu.builder().menuName("测试").createBy("admin").createTime(LocalDateTime.now()).build();
        // sysMenuDaoService.insert(systemMenu);
        // System.out.println(count);
        // System.out.println(count2);
        // System.out.println(systemMenus);

        SystemMenu systemMenu = sysMenuDaoService.selectById(11);
        System.out.println(systemMenu);

        CompletableFuture.runAsync(() -> {
            try {
                System.out.println("当前运行的线程名称：" + Thread.currentThread().getName());
                Thread.sleep(1000);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }, customThreadPoolExecutor);
    }
}
